//
//  GLProject.swift
//  GitOSC-Swift
//
//  Created by 李萍 on 16/1/4.
//  Copyright © 2016年 李萍. All rights reserved.
//

import Foundation
import SwiftyJSON

enum ProjectsType : Int {
    case ProjectsTypeFeatured = 0
    case ProjectsTypePopular
    case ProjectsTypeLatest
    case ProjectsTypeEventForUser
    case ProjectsTypeStared
    case ProjectsTypeWatched
    case ProjectsTypeLanguage
    case ProjectsTypeSearch
    case ProjectsTypeUserProjects
}

class GTProject {
    
    var projectID : Int?
    var name : String?
    var projectDescription : String?
    var defaultBranch : String?
    var owner : GTUser?
    
    var publicProject : Bool?
    var pathProject : String?
    var pathWithNameSpace : String?
    var issueEnabled : Bool?
    var pullRequestEnabled : Bool?
    
    var wikiEnabled : Bool?
    var createdAt : String?
    var lastPushAt : String?
    var parentId : Int?
    var forksCount : Int?
    
    var starsCount : Int?
    var watchesCount : Int?
    var language : String?
    var stared : Bool?
    var wacthed : Bool?
    
    var recomm : Bool?
    var realPath : String?
    var svnUrlToRepoSvn : String?
    var nameSpace : String?

    init(json:SwiftyJSON.JSON) {
            
            self.projectID = json["id"].intValue
            self.name = json["name"].stringValue
            self.projectDescription = json["description"].stringValue
            self.defaultBranch = json["default_branch"].stringValue
            self.owner = GTUser(json:json["owner"])
        
            self.publicProject = json["public"].boolValue
            self.pathProject = json["path"].stringValue
            self.pathWithNameSpace = json["path_with_namespace"].stringValue
            self.issueEnabled = json["issues_enabled"].boolValue
            self.pullRequestEnabled = json["pull_requests_enabled"].boolValue
            
            self.wikiEnabled = json["wiki_enabled"].boolValue
            self.createdAt = json["created_at"].stringValue
            self.lastPushAt = json["last_push_at"].stringValue
            self.parentId = json["parent_id"].intValue
            self.forksCount = json["forks_count"].intValue
            
            self.starsCount = json["stars_count"].intValue
            self.watchesCount = json["watches_count"].intValue
            self.language = json["language"].stringValue
            self.stared = json["stared"].boolValue
            self.wacthed = json["watched"].boolValue
            
            self.recomm = json["recomm"].boolValue
            self.realPath = json["real_path"].stringValue
            self.svnUrlToRepoSvn = json["svn_url_to_repo"].stringValue
            self.nameSpace = JSON(pathWithNameSpace!).stringValue.stringByReplacingOccurrencesOfString("/", withString: "%2F").stringByReplacingOccurrencesOfString(".", withString: "+")
    }
    
    func detailForProjectLanguage() -> NSAttributedString {//fork star watch
        let mutableAttributedString = NSMutableAttributedString()
        
        if ((language?.isEmpty) != nil) {
            let textAttachment = NSTextAttachment()
            let image = UIImage(named: "language")
            textAttachment.image = image
            textAttachment.bounds = CGRect.init(x: 0, y: -2, width: (image?.size.width)!, height: (image?.size.height)!)
            let attributedString = NSAttributedString(attachment: textAttachment)
            
            mutableAttributedString.appendAttributedString(attributedString)
            mutableAttributedString.appendAttributedString(NSAttributedString(string: " \(JSON(language!))  "))
        }
        
        if forksCount > 0 {
            let textAttachment = NSTextAttachment()
            let image = UIImage(named: "fork")
            textAttachment.image = image
            textAttachment.bounds = CGRect.init(x: 0, y: -2, width: (image?.size.width)!, height: (image?.size.height)!)
            let attributedString = NSAttributedString(attachment: textAttachment)
            
            mutableAttributedString.appendAttributedString(attributedString)
            mutableAttributedString.appendAttributedString(NSAttributedString(string: " \(JSON(forksCount!))  "))
        }
        
        if starsCount > 0 {
            let textAttachment = NSTextAttachment()
            let image = UIImage(named: "star")
            textAttachment.image = image
            textAttachment.bounds = CGRect.init(x: 0, y: -2, width: (image?.size.width)!, height: (image?.size.height)!)
            let attributedString = NSAttributedString(attachment: textAttachment)
            
            mutableAttributedString.appendAttributedString(attributedString)
            mutableAttributedString.appendAttributedString(NSAttributedString(string: " \(JSON(starsCount!))  "))
        }
        
        if watchesCount > 0 {
            let textAttachment = NSTextAttachment()
            let image = UIImage(named: "watch")
            textAttachment.image = image
            textAttachment.bounds = CGRect.init(x: 0, y: -2, width: (image?.size.width)!, height: (image?.size.height)!)
            let attributedString = NSAttributedString(attachment: textAttachment)
            
            mutableAttributedString.appendAttributedString(attributedString)
            mutableAttributedString.appendAttributedString(NSAttributedString(string: " \(JSON(watchesCount!))"))
        }
        
        return mutableAttributedString;
    }

}